/**
 * File: ArcMultiplicityMean.java
 * Created by: mhaimel
 * Created on: Apr 22, 2010
 * CVS:  $Id: ArcMultiplicityMean.java 1.0 Apr 22, 2010 11:46:13 AM mhaimel Exp $
 */
package uk.ac.ebi.curtain.model.graph.curtain.filter;

import uk.ac.ebi.curtain.model.graph.Arc;
import uk.ac.ebi.curtain.model.graph.GraphAccess.TouchEach;
import uk.ac.ebi.curtain.model.graph.curtain.CategoryReadInfo;
import uk.ac.ebi.curtain.model.graph.curtain.ContigInfo;
import uk.ac.ebi.curtain.model.graph.curtain.ReadWrapper;
import uk.ac.ebi.curtain.utils.concurrent.CurtainAtomicLongArray;

/**
 * @author mhaimel
 *
 */
public class ArcMultiplicityMean implements TouchEach<Arc<ContigInfo, ReadWrapper>> {
	private final CurtainAtomicLongArray cntArr;
	private final CurtainAtomicLongArray sumArr;
	public ArcMultiplicityMean(Integer categories) {
		cntArr = new CurtainAtomicLongArray(categories);
		sumArr = new CurtainAtomicLongArray(categories);
	}
	@Override
	public void touch(Arc<ContigInfo, ReadWrapper> arc) {
        if(null == arc)
            return;
		int categories = cntArr.length();
		for(int cat = 0; cat < categories; ++cat){
			CategoryReadInfo value = arc.getValue().getInfo(cat);
			if(null != value){
				cntArr.incrementAndGet(cat);
				sumArr.addAndGet(cat, value.getMultiplicity());
			}
		}
	}
	
	public double getMean(int category){
		long cnt = cntArr.get(category);
		if(cnt <= 0){
			return 0d;
		}
		return sumArr.get(category) /cnt; 
	}
	
	public double[] getMeans(){
		int size = cntArr.length();
		double[] arr = new double[size];
		for(int i = 0; i < size; ++i){
			arr[i] = getMean(i);
		}
		return arr;
	}
}
